package process;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.nio.channels.DatagramChannel;

import android.content.Context;

public class UDPClient {
	
	private DatagramSocket socket = null;
	private DatagramPacket data ;
	//private String Address = "192.168.2.102";
	private int port = 8282;
	public String KetNoi(String ThongTin,String[] Address)
	{
		String Chuoi = "";
		try {
			for(int i=0;i<Address.length;i++)
			{
				InetAddress IA = InetAddress.getByName(Address[i]);
				data = new DatagramPacket(ThongTin.getBytes(), ThongTin.length(), IA, port);
				socket = new DatagramSocket();
				socket.send(data);
				Chuoi = "Thành công";
				socket.close();
			}
			
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Chuoi = "Send thành công";
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			Chuoi = "Kết nối không thành công";
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Chuoi = "Kết nối không thành công";
			e.printStackTrace();
		}
		
		return Chuoi;
	}
	public static String ReadSettings(Context context, String file) throws IOException
	{
			    FileInputStream fis = null;
			    InputStreamReader isr = null;
			    String data = null;
			    fis = context.openFileInput(file);
			    isr = new InputStreamReader(fis);
			    char[] inputBuffer = new char[fis.available()];
			    isr.read(inputBuffer);
			    data = new String(inputBuffer);
			    isr.close();
			    fis.close();
			    return data;
	}
}
